package com.yfp.group.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.goods.domain.ComboGroupDTO;
import com.yfp.group.domain.EnterpriseOrder;
import com.yfp.group.domain.dto.ComboSettleDTO;
import com.yfp.group.domain.dto.EnterpriseOrderClientDTO;
import com.yfp.group.domain.dto.EnterpriseOrderDTO;
import com.yfp.group.domain.dto.SalesStatsClientDTO;
import com.yfp.group.domain.vo.*;
import com.yfp.group.service.EnterpriseOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 
 * @version 1.0
 * @date 2024-08-10 9:51
 */
@RestController
@RequestMapping("/enterpriseOrder")
@Tag(name = "企业订单")
public class EnterpriseOrderController extends BaseController {

    @Resource
    private EnterpriseOrderService enterpriseOrderService;


    @GetMapping("/clientOrder")
    @Operation(summary = "获取企业用户领取订单详情")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Successful response", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ClientOrderVO.class)))})
    public R<TableDataInfo> clientOrder(String clientId) {
        startPage();
        List<ClientOrderVO> list = enterpriseOrderService.clientOrder(clientId);
        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }


    @PostMapping("/enterpriseOrder")
    @Operation(summary = "订单管理列表")
    public R<FirmOrderVO> enterpriseOrderList(@RequestBody EnterpriseOrderDTO enterpriseOrderDTO) {
        FirmOrderVO firmOrderVO = enterpriseOrderService.enterpriseOrderList(enterpriseOrderDTO);
        return R.ok(firmOrderVO);
    }

    /**
     * 套餐数据汇总
     *
     * @param enterpriseOrderDTO
     * @return
     */
    @PostMapping("/comboCollect")
    @Operation(summary = "套餐数据汇总")
    public R<List<ComboVO>> comboCollect(@RequestBody EnterpriseOrderDTO enterpriseOrderDTO) {
        List<ComboVO> list = enterpriseOrderService.comboCollect(enterpriseOrderDTO);
        return R.ok(list);
    }

    /**
     * 订单管理列表导出
     *
     * @param response
     * @param enterpriseOrderDTO
     */
    @PostMapping("/derive/enterpriseOrder")
    @Operation(summary = "订单管理列表导出", description = "数据导出")
    @Log(title = "订单管理列表导出", businessType = BusinessType.EXPORT)
    public void deriveEnterpriseOrder(HttpServletResponse response, @ModelAttribute EnterpriseOrderDTO enterpriseOrderDTO) {
        List<EnterpriseOrderVO> list = enterpriseOrderService.deriveEnterpriseOrder(enterpriseOrderDTO);
        ExcelUtil<EnterpriseOrderVO> util = new ExcelUtil<>(EnterpriseOrderVO.class);
        util.exportExcel(response, list, "企业订单数据");
    }

    /**
     * 套餐数据汇总导出
     *
     * @param response
     * @param enterpriseOrderDTO
     */
    @PostMapping("/derive/comboCollect")
    @Operation(summary = "套餐数据汇总导出", description = "数据导出")
    @Log(title = "套餐数据汇总导出", businessType = BusinessType.EXPORT)
    public void deriveComboCollect(HttpServletResponse response, @ModelAttribute EnterpriseOrderDTO enterpriseOrderDTO) {
        List<ComboVO> list = enterpriseOrderService.comboCollect(enterpriseOrderDTO);
        ExcelUtil<ComboVO> util = new ExcelUtil<>(ComboVO.class);
        util.exportExcel(response, list, "套餐数据");
    }

    @GetMapping("/orderDetail")
    @Operation(summary = "订单详情列表")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Successful response", content = @Content(mediaType = "application/json", schema = @Schema(implementation = EnterpriseOrderClientVO.class)))})
    public R<TableDataInfo> orderDetail(EnterpriseOrderClientDTO enterpriseOrderClientDTO) {
        startPage();
        List<EnterpriseOrderClientVO> list = enterpriseOrderService.orderDetail(enterpriseOrderClientDTO);
        TableDataInfo dataTable = getDataTable(list);
        List<EnterpriseOrderClientVO> clientVOS = enterpriseOrderService.orderDetailComplement(list);
        dataTable.setRows(clientVOS);
        return R.ok(dataTable);
    }


    @GetMapping("/h5/combo/list")
    @Operation(summary = "h5-套餐清单", description = "h5订餐管理-获取套餐清单的信息")
    public R<List<ComboGroupDTO>> getGroupShelfComboList() {
        List<ComboGroupDTO> groupShelfComboList = enterpriseOrderService.getGroupShelfComboList();
        return R.ok(groupShelfComboList);
    }

    @PostMapping("/h5/combo/settle")
    @Operation(summary = "h5-套餐结算", description = "h5订餐管理-套餐结算")
    public R<String> settleCombo(@RequestBody ComboSettleDTO comboSettleDTO) {
        String orderSn = enterpriseOrderService.settleCombo(comboSettleDTO);
        return R.ok(orderSn);
    }

    @GetMapping("/h5/order/payment/wx/{orderSn}")
    @Operation(summary = "h5-立即支付-线上支付", description = "h5订单管理-立即支付")
    public R<Object> orderPaymentWx(@Schema(description = "订单编号") @PathVariable String orderSn,
                                    @Schema(description = "支付方式") @Parameter(name = "payType") Integer payType,
                                    @Schema(description = "微信用户openId") @Parameter(name = "openId") String openId) {
        Object jsapiResult = enterpriseOrderService.orderPaymentWx(orderSn, payType, openId);
        return R.ok(jsapiResult);
    }

    @GetMapping("/h5/order/payment/balance/{orderSn}")
    @Operation(summary = "h5-立即支付-余额支付", description = "h5订单管理-立即支付")
    public R<Void> orderPaymentBalance(@Schema(description = "订单编号") @PathVariable String orderSn,
                                       @Schema(description = "支付方式") @Parameter(name = "payType") Integer payType) {
        enterpriseOrderService.orderPaymentBalance(orderSn, payType);
        return R.ok();
    }

    @GetMapping("/h5/order/payment/status/{orderSn}")
    @Operation(summary = "h5-商户订单号查询订单状态", description = "查询订单状态,若订单状态改变则更新订单状态")
    public R<EnterpriseOrder> getOrderStatus(@PathVariable String orderSn) {
        EnterpriseOrder orderStatus = enterpriseOrderService.getOrderStatus(orderSn);
        return R.ok(orderStatus);
    }

    @GetMapping("/h5/order/payment/info/{orderSn}")
    @Operation(summary = "h5-支付订单详情", description = "h5订单管理-支付订单详情")
    public R<EnterpriseOrderPayInfoVO> orderPaymentInfo(@Schema(description = "订单编号") @PathVariable String orderSn) {
        EnterpriseOrderPayInfoVO enterpriseOrderPayInfoVO = enterpriseOrderService.orderPaymentInfo(orderSn);
        return R.ok(enterpriseOrderPayInfoVO);
    }

    @GetMapping("/h5/order/QrCode/info")
    @Operation(summary = "h5-订单-领餐二维码", description = "h5订单-领餐二维码-今日订单只有一个")
    public R<EnterpriseOrderQrCodeInfoVO> orderQrCodeInfo() {
        EnterpriseOrderQrCodeInfoVO enterpriseOrderQrCodeInfoVO = enterpriseOrderService.orderQrCodeInfo();
        return R.ok(enterpriseOrderQrCodeInfoVO);
    }

    @GetMapping("/h5/order/QrCode/info/{orderSn}")
    @Operation(summary = "h5-订单-领餐二维码", description = "h5订单-领餐二维码-根据orderSn查询")
    public R<EnterpriseOrderQrCodeInfoVO> orderQrCodeInfoByOrderSn(@Schema(description = "订单编号") @PathVariable String orderSn) {
        EnterpriseOrderQrCodeInfoVO enterpriseOrderQrCodeInfoVO = enterpriseOrderService.orderQrCodeInfoByOrderSn(orderSn);
        return R.ok(enterpriseOrderQrCodeInfoVO);
    }

    @GetMapping("/h5/order/list")
    @Operation(summary = "h5-订单列表", description = "h5订单管理-订单列表")
    public R<List<EnterpriseOrderListVO>> orderList(@Parameter(name = "startTime") Date startTime, @Parameter(name = "endTime") Date endTime) {
        List<EnterpriseOrderListVO> enterpriseOrderListVOS = enterpriseOrderService.orderList(startTime, endTime);
        return R.ok(enterpriseOrderListVOS);
    }

    @DeleteMapping("/h5/order/cancel/{orderSn}")
    @Operation(summary = "h5-取消订单", description = "h5订单管理-取消订单")
    public R<Void> orderCancel(@Schema(description = "订单编号") @PathVariable String orderSn) {
        enterpriseOrderService.orderCancel(orderSn);
        return R.ok();
    }

    @GetMapping("/h5/order/info/{orderSn}")
    @Operation(summary = "h5-订单详情", description = "h5订单管理-订单详情")
    public R<EnterpriseOrderInfoVO> orderInfo(@Schema(description = "订单编号") @PathVariable String orderSn) {
        EnterpriseOrderInfoVO enterpriseOrderInfoVO = enterpriseOrderService.orderInfo(orderSn);
        return R.ok(enterpriseOrderInfoVO);
    }

    @GetMapping("/h5/sales/stats/combo")
    @Operation(summary = "h5-订餐详情-套餐份数列表", description = "h5订餐详情-套餐份数-页面上半部分")
    public R<List<SalesStatsComboVO>> salesStatsCombo(@Parameter(name = "date") Date date) {
        List<SalesStatsComboVO> salesStatsComboVO = enterpriseOrderService.salesStatsCombo(date);
        return R.ok(salesStatsComboVO);
    }

    @PostMapping("/h5/sales/stats/client")
    @Operation(summary = "h5-订餐详情-房间号列表", description = "h5订餐详情-房间号-页面下半部分")
    public R<List<SalesStatsClientVO>> salesStatsClient(@RequestBody SalesStatsClientDTO salesStatsClientDTO) {
        List<SalesStatsClientVO> salesStatsClientVOS = enterpriseOrderService.salesStatsClient(salesStatsClientDTO);
        return R.ok(salesStatsClientVOS);
    }

    @PostMapping("/updateFinishTime")
    @Operation(summary = "修改取餐完成时间")
    public R<Void> updateFinishTime(@RequestParam("enterpriseOrderSn") String enterpriseOrderSn){
        enterpriseOrderService.updateFinishTime(enterpriseOrderSn);
        return R.ok();
    }

}
